package com.oolatina.fragments;


import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oolatina.R;
import com.oolatina.RadioView;
import com.oolatina.SCAudioPlayer;
import com.oolatina.SCButton;
import com.oolatina.SCImage;
import com.oolatina.SCItunesPartner;
import com.oolatina.SCLabel;
import com.oolatina.SCNetwork;
import com.oolatina.activity.MainActivity;
import com.oolatina.activity.YoutubeView;
import com.oolatina.adapters.ArtistGridAdapter;

import android.os.AsyncTask;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;

public class AudioFragment extends BaseFragment implements OnItemClickListener {


	private int[] videoIDs;
	private GridView gridview;
	private ServerTask task;
	private ArrayList<HashMap<String, String>> mData;
	private ArtistGridAdapter adapter;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_videoview, null);
		videoIDs = mView.getContext().getResources().getIntArray(R.array.video_ids);
		gridview = (GridView) mView.findViewById(R.id.gridview);
	    gridview.setOnItemClickListener(this);
	    gridview.setVisibility(View.GONE);
	    mView.findViewById(R.id.progress).setVisibility(View.VISIBLE);
	    getData();
	    
		return mView;
	}
	
	private void getData() {
		String url = "http://www.oolatina.com/webservice/oolatina_api.php?command=GET_ALL_ARTIST_AUDIO";
		task = new ServerTask(this);
		task.execute(url);
	}
	
	private class ServerTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>>{
		
		private AudioFragment mainContext;

		ServerTask(AudioFragment mainContext){
			this.mainContext = mainContext;
		}
		
		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
			SCNetwork net = new SCNetwork();
			ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String,String>>();
			
	        String json = net.GetJSON(params[0]);
	        try
	        {
	        	//JSONObject jsonObject = new JSONObject(json);    
	        	JSONArray jsonArray = new JSONArray(json);
	        	
	        	for(int i=0; i< jsonArray.length(); i++){
	        		HashMap<String, String> data = new HashMap<String, String>();
	                JSONObject dJson = jsonArray.getJSONObject(i);
	                data.put("label", dJson.getString("artistname"));
	                data.put("artistid", dJson.getString("artistid"));
	                data.put("image", dJson.getString("image"));
	                listData.add(data);
	        	}
	        } 
	        catch(Exception e)
	        {
	        	e.printStackTrace();
	        }
	        
			return listData;
		}
		
		@Override
		protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
			mainContext.onData(result);
			super.onPostExecute(result);
		}
		
	}
	

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		String artistID = mData.get(position).get("artistid");
		AlbumFragment frag = new AlbumFragment();
		Bundle args = new Bundle();
		args.putString("DATA", artistID);
		frag.setArguments(args);
		((MainActivity) getActivity()).showNextScreen(frag);
		
	}

	public void onData(ArrayList<HashMap<String, String>> result) {
		mData = result;
		adapter = new ArtistGridAdapter(mView.getContext(),mData);
		mView.findViewById(R.id.progress).setVisibility(View.GONE);
		gridview.setVisibility(View.VISIBLE);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(this);
		
	}
	
}
